package org.mspring.search.listener;

import javax.annotation.PostConstruct;

import org.hibernate.SessionFactory;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用于加载Hibernate时间监听
 *
 * @author Gao Youbo
 * @since 2013年7月31日
 */
@Component
public class HibernateEventWiring {

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private LuceneInsertEventListener luceneInsertEventListener;
    @Autowired
    private LuceneUpdateEventListener luceneUpdateEventListener;
    @Autowired
    private LuceneDeleteEventListener luceneDeleteEventListener;

    @PostConstruct
    public void registerListeners() {
        EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(luceneInsertEventListener);
        registry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(luceneUpdateEventListener);
        registry.getEventListenerGroup(EventType.POST_DELETE).appendListener(luceneDeleteEventListener);
    }
}
